package com.example.CollaborativeDiscussionLearning.controller;

import com.example.CollaborativeDiscussionLearning.dto.ChatUserDTO;
import com.example.CollaborativeDiscussionLearning.entity.ChatUser;
import com.example.CollaborativeDiscussionLearning.entity.Course;
import com.example.CollaborativeDiscussionLearning.entity.CourseStudent;
import com.example.CollaborativeDiscussionLearning.service.CourseStudentService.CourseStudentService;
import com.example.CollaborativeDiscussionLearning.service.chatRoomService.ChatRoomService;
import com.example.CollaborativeDiscussionLearning.service.courseService.CourseService;
import com.example.CollaborativeDiscussionLearning.vo.CoureStudentVo;
import com.example.CollaborativeDiscussionLearning.vo.ResponseDataVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.validation.constraints.Pattern;

/**
 * 课程管理控制层
 * */
@Controller
@Validated
@Slf4j
public class CourseController {
    @Autowired
    private CourseService courseService;
    @Autowired
    private CourseStudentService courseStudentService;
    @Autowired
    private ChatRoomService chatRoomService;

    /**
     * 新增课程
     * */
    @PostMapping("/courseInsert")
    @ResponseBody
    public ResponseDataVO insert(@RequestParam String name, @RequestParam @Pattern(regexp = "^[0-9]{10}$") String code, @RequestParam String teacher){
        Course course = new Course();
        course.setName(name);
        course.setTeacher(teacher);
        course.setTeacherCode(code);
        try{
            if(courseService.query(course)!=null){
                log.info(courseService.query(course).toString());
                return ResponseDataVO.fail("已经存在该课程");
            }else{
                //检查老师是否存在
                ChatUserDTO chatUserDTO=ChatUserDTO.builder().name(teacher).build();

                //chatRoomService.queryUser()
                Integer res = courseService.insert(course);
                if(res==0){
                    return ResponseDataVO.success("新增失败") ;
                }
                return ResponseDataVO.success(res);
            }
        }catch (Exception e){
            e.printStackTrace();
            return ResponseDataVO.fail("新增错误");
        }

    }

    /**
     * 查询课程接口
     * */
    @PostMapping("/queryCourse")
    @ResponseBody
    public ResponseDataVO queryCourse(@RequestParam String name, @RequestParam String teacher) {
        Course course = new Course().setName(name).setTeacher(teacher);
        Course exitCourse = courseService.query(course);
        if(exitCourse!=null){
            return ResponseDataVO.success(exitCourse);
        }else{
            return ResponseDataVO.fail(201,"查询的课程不存在");
        }
    }

    /**
     * 删除课程接口
     * */
    @PostMapping("/deleteCourse")
    @ResponseBody
    public ResponseDataVO deleteCourse(@RequestParam  String name, @RequestParam String teacher) {

        Course course = new Course().setName(name).setTeacher(teacher);
        Course exitCourse = courseService.query(course);
        if (exitCourse != null) {
            int res = courseService.delete(exitCourse.getId());
            return ResponseDataVO.success("删除成功");
        } else {
            return ResponseDataVO.fail(201, "查询的课程不存在");
        }
    }

    /**
     * 新增课程-学生
     * */
    @PostMapping("/InsertCourseStudent")
    @ResponseBody
    public ResponseDataVO InsertCourseStudent(@RequestParam String studentCode, @RequestParam  String course, @RequestParam String teacher){
        ChatUser student =  chatRoomService.queryUserByCode(studentCode);
        Course course1 = new Course().setName(course).setTeacher(teacher);
        Course exitCourse = courseService.query(course1);
        try{
            if(student == null || student.getRole() !=2){
                return ResponseDataVO.fail("该学生不存在");
            }else if(exitCourse == null) {
                return ResponseDataVO.fail("该课程不存在");
            }else{
                //检查是否已经存在这个学生-课程关系
                CourseStudent courseStudent = new CourseStudent().setStudentCode(studentCode).setCourseCode(exitCourse.getId());
                CourseStudent exitCourseStudent = courseStudentService.queryCourseStudent(courseStudent);
                if(exitCourseStudent!=null){
                    return ResponseDataVO.fail("已经存在该学生-课程关系");
                }else{
                    return ResponseDataVO.success(courseStudentService.insert(courseStudent));
                }
            }
        }catch (Exception e){
            e.printStackTrace();
            return ResponseDataVO.fail("新增学生-课程错误");
        }
    }

    /**
     * 查询学生-课程接口
     * */
    @PostMapping("/queryCourseStudent")
    @ResponseBody
    public ResponseDataVO queryCourseStudent(@RequestParam String studentCode, @RequestParam Integer courseCode) {
        CourseStudent courseStudent = new CourseStudent().setStudentCode(studentCode).setCourseCode(courseCode);
        CourseStudent exitCourseStudent = courseStudentService.queryCourseStudent(courseStudent);
        if(exitCourseStudent!=null){
            CoureStudentVo coureStudentVo = new CoureStudentVo();
            //课程编号
            coureStudentVo.setCoureCode(courseStudent.getId());
            //学生编号
            coureStudentVo.setStudentCode(exitCourseStudent.getStudentCode());
            ChatUser user = chatRoomService.queryUserByCode(exitCourseStudent.getStudentCode());
            //学生姓名
            coureStudentVo.setStudent(user.getName());
            Course course = courseService.queryById(courseStudent.getCourseCode());
            //课程名
            coureStudentVo.setCourseName(course.getName());
            //老师
            coureStudentVo.setTeacher(course.getTeacher());
            log.info(coureStudentVo.toString());
            return ResponseDataVO.success(coureStudentVo);
        }else{
            return ResponseDataVO.fail(201,"查询的课程不存在");
        }
    }
    /**
     * 删除学生-课程接口
     * */
    @PostMapping("/deleteCourseStudent")
    @ResponseBody
    public ResponseDataVO deleteCourseStudent(@RequestParam  String studentCode, @RequestParam Integer courseCode) {

        CourseStudent courseStudent = new CourseStudent().setCourseCode(courseCode).setStudentCode(studentCode);
        CourseStudent exitCourseStudent = courseStudentService.queryCourseStudent(courseStudent);
        if (exitCourseStudent != null) {
            int res = courseService.delete(exitCourseStudent.getId());
            return ResponseDataVO.success("删除成功");
        } else {
            return ResponseDataVO.fail(201, "查询的学生-课程关系不存在");
        }
    }

}
